<template>
  <div class="main">
    <div class="loginBox">
      <div class="tabBoxSwitch">
        <ul class="tabBoxSwitchUl">
          <li :class="tabSelected === index?'tab-active':''" v-for="(txt,index) in tabList" :key="index"
              @click="tabSelected=index">{{ txt }}
          </li>
        </ul>
      </div>
      <div v-show="tabSelected===0">
        <el-form :model="userForm" :rules="userRoles" ref="userForm">
          <el-form-item style="margin-top: 24px;" prop="username" :error="userFormError.username">
            <el-input placeholder="用户名或手机号" v-model="userForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password" :error="userFormError.password">
            <el-input placeholder="密码" v-model="userForm.password" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="medium" @click="submitForm('userForm')">登 录</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div v-show="tabSelected===1">
        <el-form :model="smsForm" :rules="smsRoles" ref="smsForm">
          <el-form-item style="margin-top: 24px;" prop="phone">
            <el-input placeholder="手机号" v-model="smsForm.phone"></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <el-row type="flex" justify="space-between">
              <el-input placeholder="验证码" v-model="smsForm.code" show-password></el-input>
              <el-button style="margin-left: 10px;" @click="sendSmsCode" :disabled="btnSmsDisabled">{{ btnSmsText }}
              </el-button>
            </el-row>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="medium" @click="submitForm('smsForm')">登 录</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      tabSelected: 0,
      tabList: ['密码登录', '免密登录'],
      userForm: {
        username: "",
        password: ""
      },
      userFormError: {
        username: "",
        password: ""
      },
      userRoles: {
        username: [
          {required: true, message: '请输入用户名或手机号', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
        ],
      },
      smsForm: {
        phone: "",
        code: '',
      },
      smsRoles: {
        phone: [
          {required: true, message: '请输入手机号', trigger: 'blur'},
          {pattern: /^1[3-9]\d{9}$/, message: '手机号格式错误', trigger: 'blur'}
        ],
        code: [
          {required: true, message: '请输入验证码', trigger: 'blur'},
        ],
      },
      btnSmsDisabled: false,
      btnSmsText: '发送验证码',
    };
  },
  methods: {
    submitForm(formName) {
      this.clearCustomFormError();
      this.$refs[formName].validate((valid) => {
        if (!valid) {
          // console.log('验证失败');
          return false;
        }
        // console.log('验证通过')
        this.axios.post("/base/auth/", this.userForm).then(res => {
          if (res.data.code === 0) {
            this.$store.commit("login", res.data.data);
            this.$router.push({path: "/"});
            console.log(this.$store.state.token)
            return;
          }
          if (res.data.code === 1000) {
            this.validateFormFailed(res.data.detail);
            return;
          }
          if (res.data.code === 1001) {
            this.$message.error(res.data.detail);
          } else {
            this.$message.error("请求失败")
          }
        })
      });
    },
    sendSmsCode() {
      this.$refs.smsForm.validateField('phone', (error) => {
        if (error) {
          return false;
        }
        this.btnSmsDisabled = true;
        let txt = 60;
        let interval = window.setInterval(() => {
          txt -= 1;
          this.btnSmsText = `${txt}秒后重新发送`;
          if (txt < 1) {
            this.btnSmsText = '重新发送';
            this.btnSmsDisabled = false;
            window.clearInterval(interval);
          }
        }, 1000)

      })
    },
    validateFormFailed(errorData) {
      for (let fieldName in errorData) {
        let error = errorData[fieldName][0];
        this.userFormError[fieldName] = error;
      }
    },
    clearCustomFormError() {
      for (let key in this.userFormError) {
        this.userFormError[key] = ""
      }
    }
  }
}
</script>

<style scoped>
.main {
  width: 100%;
  height: 100vh;
  background-color: #2E3E9C;

  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.loginBox {
  background-color: #FFFFFF;
  box-shadow: 0 1px 3px rgba(26, 26, 26, 0.1);
  border-radius: 2px;
  width: 380px;
  min-height: 200px;
  padding: 0 24px 20px;
}

.tabBoxSwitch .tabBoxSwitchUl {
  list-style: none;
  padding: 0;
  margin: 0;
}

.tabBoxSwitch .tabBoxSwitchUl li {
  display: inline-block;
  height: 60px;
  font-size: 16px;
  line-height: 60px;
  margin-right: 24px;
  cursor: pointer;
}

.tab-active {
  position: relative;
  color: #1a1a1a;
  font-weight: 600;
  font-synthesis: style;
}


.tab-active::before {
  display: block;
  position: absolute;
  bottom: 0;
  content: "";
  width: 100%;
  height: 3px;
  background-color: #0084ff;
}
</style>